<template>
<div id="defeat">
  <div v-show="false">{{this.time}}</div>
  <component-title :title-text="'本月跑'+(defeat>=0?'赢':'输')+'沪深300'"></component-title>
  <div v-show="module==0">
    <div class="defeatDetails" :style="{color:defeat>=0?'#eb333b':'#1aae52'}" v-text="(defeat*100).toFixed(2)+'%'"></div>
    <ul class="chartDefeat">
      <li class="dataBar" v-for="item in defeatData">
        <div class="barName">{{item.text}}</div>
        <div class="barContainer" :style="{borderColor:item.number>=0?'#eb333b':'#1aae52'}">
          <div class="circleLeft" :style="{background:item.number>=0?'#eb333b':'#1aae52'}"></div>
          <div class="barContent" :style="{width:Math.abs(item.number)*100*widthRate+'%',backgroundColor:item.number>=0?'#eb333b':'#1aae52'}"></div>
          <div class="circleRight" :style="{background:item.number>=0?'#eb333b':'#1aae52'}"></div>
        </div>
        <div class="barNumber" :style="{color:item.number>=0?'#eb333b':'#1aae52'}" v-text="(item.number*100).toFixed(2)+'%'"></div>
      </li>
    </ul>
  </div>
  <return-error v-show="module==1"></return-error>
  <return-empty v-show="module==2"></return-empty>
</div>
</template>
<script>
import Title from '@/components/Title'
import RemoteDAO from '@/common/remoteDAO'

export default {
  props: ['time', 'monthstatus'],
  data() {
    return {
      module: 0,
      defeatData: [{
          name: 'my',
          text: '我',
          number: 0
        },
        {
          name: 'hushen300',
          text: '沪深300',
          number: 0
        }
      ]
    }
  },
  computed: {
    defeat() {
      return this.defeatData[0].number - this.defeatData[1].number;
    },
    widthRate: function() {
      let originArr = []
      this.defeatData.forEach(value => {
        originArr.push(Math.abs(value.number) * 100)
      })
      let maxNumber = Math.max.apply(null, originArr)
      if (maxNumber < 80) {
        return 80 / maxNumber
      } else {
        return 1
      }
    }
  },
  components: {
    'component-title': Title
  },
  created() {
    this.initEvent();
  },
  watch: {
    time: function() {
      this.initEvent();
    },
    monthstatus: function() {
      this.initEvent();
    }
  },
  methods: {
    initEvent() {
      this.module = 0
      var self = this;
      var obj = {
        "pagenum": "30",
        "recent": this.time
      };
      RemoteDAO.callZL13100040(obj, self.ZL13100040);
      RemoteDAO.callZL13100041(obj, self.ZL13100041);
    },
    ZL13100040(result) {
      if (result.error == '1') {
        this.module = 1
      } else {
        let data = result.data;
        let bigdata = JSON.parse(data).body[0].bigdata;
        let specData = JSON.parse(bigdata).hits.hits;
        if (specData.length == 0) {
          this.module = 2
        } else {
          this.defeatData[0].number = specData[0]._source.total_profit_total_rate;
        }
      }
    },
    ZL13100041(result) {
      if (result.error == '1') {
        this.module = 1
      } else {
        let data = result.data;
        let bigdata = JSON.parse(data).body[0].bigdata;
        let specData = JSON.parse(bigdata).hits.hits;
        if (specData.length == 0) {
          this.module = 2
        } else {
          this.defeatData[1].number = specData[0]._source.hs300_total_profit_rate;
        }
      }
    }
  }
}
</script>
<style scoped>
#defeat {
  padding-top: 80px;
  background-color: #fff;
}

.defeatDetails {
  height: 60px;
  margin-top: 20px;
  font-size: 60px;
  line-height: 60px;
  color: #1ea241;
  text-align: center;
}

.charts {
  padding: 0 30px;
}

.chartDefeat {
  margin-top: 14px;
}

.dataBar {
  margin-top: 20px;
  height: 40px;
  padding: 5px 40px;
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dataBar .barName {
  font-size: 28px;
  width: 120px;
  color: #555;
  padding-right: 30px;
}

.barContainer {
  height: 30px;
  background: #f1f1f1;
  box-sizing: border-box;
  border-radius: 15px;
  overflow: hidden;
  flex-grow: 1;
}

.circleLeft {
  width: 15px;
  height: 30px;
  border-radius: 15px 0 0 15px;
  float: left;
}

.barContent {
  height: 100%;
  float: left;
  transition: 0.5s;
}

.circleRight {
  width: 15px;
  height: 30px;
  border-radius: 0 15px 15px 0;
  float: left;
}

.barNumber {
  width: 96px;
  font-size: 24px;
  float: right;
  line-height: 30px;
  margin-right: 6px;
  text-align: right;
}
</style>
